package controller;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import modle.bo.AddGoodsBO;
import modle.vo.GetGoodsByTypeVO;
import modle.Type;
import service.GoodsService;
import service.GoodsServiceImpl;
import util.FileUploadUtils;
import util.HttpUtils;
import util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZeYu
 * @Date: 2022/3/15 20:35
 */
@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService=new GoodsServiceImpl();
    private Gson gson=new Gson();





    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("imgUpload".equals(op)){
            imgUpload(request,response);
        }else if ("addGoods".equals(op)){
            addGoods(request,response);
        }
    }

    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {



        String requestBody = HttpUtils.getRequestBody(request);


        AddGoodsBO addGoodsBO=null;
        try {
            addGoodsBO = gson.fromJson(requestBody, AddGoodsBO.class);

        }catch (Exception e){
            response.getWriter().println(gson.toJson(Result.error("参数必须为数字")));
            return;
        }
        goodsService.addGoods(addGoodsBO);
        response.getWriter().println(gson.toJson(Result.ok()));


    }

    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> map = FileUploadUtils.parseRequest(request);
        String file = map.get("file");
        response.getWriter().println(gson.toJson(Result.ok(file)));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("getType".equals(op)){
            getType(request,response);
        }else if("getGoodsByType".equals(op)){
            getGoodsByType(request,response);
        }
    }

    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        if (StringUtils.isEmpty(typeId)){
            response.getWriter().println(gson.toJson(Result.error("参数不能为空")));
            return;

        }
        List<GetGoodsByTypeVO>goodsByTypeVOS =  goodsService.getGoodsByType(Integer.parseInt(typeId));
        response.getWriter().println(gson.toJson(Result.ok(goodsByTypeVOS)));






    }

    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList= goodsService.getType();
        response.getWriter().println(gson.toJson(Result.ok(typeList)));
    }
}
